﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace ConsoleTest
{
    enum test
    {
        a, b, c
    }

    //abstract class bb
    //{
    //    public int A { get; private set; }
    //    public bb(int a)
    //    {
    //        A = a;
    //    }
    //}

    //class aa : bb { 
    //aa必须要实现bb的其中一个架造方法才行
    //}



    class Program
    {
        static void Main(string[] args)
        {
            //Entity entity = new Entity();
            //People peo = new People(entity, "zzl");
            //PeopleChina cp = new PeopleChina(entity);
            //peo.Create();
            using (FileStream fs = new FileStream("c:\\1.htm", FileMode.Open)) //文件用完需要释放
            {
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, (int)fs.Length);
                string s = System.Text.Encoding.GetEncoding("utf-8").GetString(bytes);
                File.AppendAllText("c:\\1.txt", s);
            }


            Console.WriteLine(test.a.ToString());

            List<int> arr = new List<int> { 1, 2, 3, 4 };
            Console.WriteLine(arr.Where(i => i == 5).FirstOrDefault());



            StringBuilder str = new StringBuilder();
            //查看
            str.Append(@" function seeFun(id) {
            $.ajax({
                type: 'POST',
                url: '/controllers/actions',
                data: { userID: id },
                success: function (data) {
                    $('#detail').html(data);
                }
            });
            art.dialog({
                title: '详细信息',
                content: document.getElementById('detail'),
                id: 'D345'
            });
        }");
            str.Replace("controllers", "zzl");
            Console.WriteLine(str.ToString());
            Console.ReadKey();
            UserBases u = new UserBases("占岭");
            u.OnCreate();//先订阅
            u.Create();//再触发
            Console.ReadKey();
        }
    }

    #region 普通事件的实现
    public class PeopleChina
    {
        public PeopleChina(Entity entity)
        {
            entity.CreateBefore += new ConsoleTest.Entity.CreateEntityEventHandler(this.CreatePeopleChinaBefore);
            entity.CreateAfter += new ConsoleTest.Entity.CreateEntityEventHandler(this.CreatePeopleChinaAfter);

        }

        public void CreatePeopleChinaBefore(object sender, CreateEntityEventArgs e)
        {
            Console.WriteLine("建立之前" + e.Name);
        }

        public void CreatePeopleChinaAfter(object sender, CreateEntityEventArgs e)
        {
            Console.WriteLine("建立之后" + e.Name);
        }

    }
    public class People : Entity
    {
        string _name;
        Entity _entity;
        public People(Entity entity, string name)
        {
            _name = name;
            _entity = entity;
        }
        public People()
        {

        }
        public void Create()
        {
            _entity.OnCreateBefore(_name);
            Console.WriteLine("建立一个People对象");
            _entity.OnCreateAfter(_name);
        }
    }
    public class CreateEntityEventArgs
    {
        public CreateEntityEventArgs(string name)
        {
            this.Name = name;
        }
        public string Name { get; set; }
    }
    /// <summary>
    /// 实体对象
    /// </summary>
    public class Entity
    {
        #region Delegates and Events
        /// <summary>
        /// 建立实体时的委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public delegate void CreateEntityEventHandler(object sender, CreateEntityEventArgs e);
        /// <summary>
        /// 建立实体之前触发事件
        /// </summary>
        public event CreateEntityEventHandler CreateBefore;
        /// <summary>
        /// 建立实体之后触发事件
        /// </summary>
        public event CreateEntityEventHandler CreateAfter;
        #endregion

        #region Protected Virtual Methods
        /// <summary>
        /// 触发CreateBefore事件
        /// </summary>
        /// <param name="name"></param>
        public virtual void OnCreateBefore(String name)
        {
            if ((this.CreateBefore != null))
            {
                this.CreateBefore(this, new CreateEntityEventArgs(name));
            }
        }
        /// <summary>
        /// 触发CreateAfter事件
        /// </summary>
        /// <param name="name"></param>
        public virtual void OnCreateAfter(String name)
        {
            if ((this.CreateAfter != null))
            {
                this.CreateAfter(this, new CreateEntityEventArgs(name));
            }
        }
        #endregion

    }
    #endregion

    #region 抽象基类中的事件(.net linq to sql中的事件的定义是否是为了在分部类中实现（属性变更事件）之后所要自动处理的逻辑呢)
    public abstract class EntityBase
    {
        #region Delegates and Events
        /// <summary>
        /// 建立实体时的委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public delegate void CreateEntityEventHandler(object sender, CreateEntityEventArgs e);
        /// <summary>
        /// 建立实体之前触发事件
        /// </summary>
        public event CreateEntityEventHandler CreateBefore;
        /// <summary>
        /// 建立实体之后触发事件
        /// </summary>
        public event CreateEntityEventHandler CreateAfter;
        #endregion

        #region Protected Virtual Methods
        /// <summary>
        /// 触发CreateBefore事件
        /// </summary>
        /// <param name="name"></param>
        protected virtual void OnCreateBefore(String name)
        {
            if ((this.CreateBefore != null))
            {
                this.CreateBefore(this, new CreateEntityEventArgs(name));
            }
        }
        /// <summary>
        /// 触发CreateAfter事件
        /// </summary>
        /// <param name="name"></param>
        protected virtual void OnCreateAfter(String name)
        {
            if ((this.CreateAfter != null))
            {
                this.CreateAfter(this, new CreateEntityEventArgs(name));
            }
        }
        #endregion

    }

    public partial class UserBases : Entity
    {
        string _name;
        public UserBases()
        {
        }
        public UserBases(string name)
        {
            _name = name;
        }

        public void Create()
        {
            base.OnCreateBefore(_name);
            Console.WriteLine("建立一个People对象");
            base.OnCreateAfter(_name);
        }
    }

    public partial class UserBases
    {
        public void OnCreate()
        {
            base.CreateBefore += new CreateEntityEventHandler(this.CreateUserBasesBefore);
            base.CreateAfter += new CreateEntityEventHandler(this.CreateUserBasesAfter);
        }
        public void CreateUserBasesBefore(object sender, CreateEntityEventArgs e)
        {
            Console.WriteLine("Before:建立实体之前要处理的逻辑" + e.Name);
        }

        public void CreateUserBasesAfter(object sender, CreateEntityEventArgs e)
        {
            Console.WriteLine("After:建立实体之后要处理的逻辑" + e.Name);
        }
    }

    #endregion

}
